﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HMS_Hospital_Team2._6.BL;
using HMS_Hospital_Team2._6.DA;
using HMS_Hospital_Team2._6.DO;
using DevExpress.Utils;

namespace HMS_Hospital_Team2._6.GUI
{
    public partial class UcXacNhanThuTien : UserControl
    {

        //khai bao cac bien can thiet
        string stendangnhap = "";//bien lay ten dang nhap 
        string smk = "";//bien lay mat khau 
        public string smapb = "";//bien lay Ma phong ban
        string sMaBan = "";//bien lay Ma ban

        HoaDonBL xacnhantt = new HoaDonBL();
        ChuyenTien chuyentien = new ChuyenTien();

        public UcXacNhanThuTien(string sTenDN, string sMatKhau, string sMapb, string sMaban)
        {
            stendangnhap = sTenDN;
            smk = sMatKhau;
            smapb = sMapb;
            sMaBan = sMaban;
            InitializeComponent();
        }

        private void UcXacNhanThuTien_Load(object sender, EventArgs e)
        {
            Loaddshoadon();
        }
        //bat su kien khi ng dung chon bat ki dong nao tren danh sach hoa don
        private void grdDShoadonchuathu_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            loadhoadondangchon();
            Decimal itong = 0;
            for (int i = 0; i < grddsHoaDon_XacNhanTT.RowCount; i++)
            {
                itong = itong + Decimal.Parse(grddsHoaDon_XacNhanTT.GetRowCellValue(i, grddsHoaDon_XacNhanTT.Columns[2]).ToString());
            }
            txtTienphaitra_XacNhanThuTien.Text = String.Format("{0:0,0}", itong);
            grdcltongtien.DisplayFormat.FormatType = FormatType.Numeric;
            grdcltongtien.DisplayFormat.FormatString = "{0:0,0} VND";
            txtTongtien_XacNhanThuTien.Text = String.Format("{0:0,0}", itong);

        }

        //bat su kien khi ng dung 


        //bat su kien khi ng dung nhan vao nut "Đã Thu Tiền"
        private void btnDathutien_XacNhanThuTien_Click(object sender, EventArgs e)
        {
            int icount = grddsHoaDon_XacNhanTT.RowCount;
            if (icount == 0)
            {
                MessageBox.Show("Vui lòng chọn hóa đơn trước khi nhấn nút 'Đã Thu Tiền'.", "Thông báo", MessageBoxButtons.OK);
            }
            else
            {
                if (txtTiennhan_XacNhanThuTien.Text.Trim() == ""|| txtTienthua_XacNhanThuTien.Text.Trim()=="")
                {
                    MessageBox.Show("Vui lòng kiểm tra Tiền nhận hoặc Tiền thừa!", "Thông báo", MessageBoxButtons.OK,MessageBoxIcon.Warning);
                }
                else
                {
                    //cap nhat vao csdl
                    for (int i = 0; i < grddsHoaDon_XacNhanTT.RowCount; i++)
                    {
                        string sMaHD = grddsHoaDon_XacNhanTT.GetRowCellValue(i, grddsHoaDon_XacNhanTT.Columns[0]).ToString();
                        xacnhanthutien(sMaHD);
                    }
                    // lam sach cac textbox, combobox...
                    txtMabn_XacNhanThuTien.Text = null;
                    txtTenbn_XacNhanThuTien.Text = null;
                    txtTiennhan_XacNhanThuTien.Text = null;
                    txtTienthua_XacNhanThuTien.Text = null;
                    txtTongtien_XacNhanThuTien.Text = null;
                    txtTienphaitra_XacNhanThuTien.Text = null;
                    txtTuoi_XacNhanThuTien.Text = null;
                    cmbGioiTinh_XacNhanThuTien.SelectedItem = null;

                    for (int i = 0; i < grddsHoaDon_XacNhanTT.RowCount; )
                    {
                        grddsHoaDon_XacNhanTT.DeleteRow(i);
                    }
                    //Load lai gridview
                    grdHoadondathu_XacNhanThuTien.DataSource = xacnhantt.LoadHDdathu_BL(sMaBan);
                    Loaddshoadon();
                }
            }
        }

        //bat su kien khi ng dung nhan enter trong textbox tien nhan
        private void txtTiennhan_XacNhanThuTien_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                if (txtTiennhan_XacNhanThuTien.Text.Trim() == "")
                {
                    MessageBox.Show("Vui lòng nhập số tiền nhận!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    string stiennhan = String.Format("{0:0,0}", Decimal.Parse(txtTiennhan_XacNhanThuTien.Text));
                    Decimal itiennhan = Decimal.Parse(stiennhan);
                    Decimal itienphaitra = Decimal.Parse(txtTienphaitra_XacNhanThuTien.Text);
                    if (itiennhan >= itienphaitra)
                    {
                        //tinh tien thua va hien thi ra tren textbox tien thua
                        txtTienthua_XacNhanThuTien.Text = String.Format("{0:0,0}", (itiennhan - itienphaitra));
                        txtTiennhan_XacNhanThuTien.Text = String.Format("{0:0,0}", itiennhan);
                    }
                    else
                    {
                        MessageBox.Show("Vui lòng kiểm tra lại Số tiền nhận!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        //Ham load danh sach cac hoa don chua thu tien va da thu
        void Loaddshoadon()
        {
            grdBienlaichuathu_XacNhanThuTien.DataSource = DA.HoaDonDA.LaydshdBN_DA(sMaBan);
            grdHoadondathu_XacNhanThuTien.DataSource = xacnhantt.LoadHDdathu_BL(sMaBan);
        }

        //Ham xac nhan da thu tien 
        void xacnhanthutien(string smahd)
        {
            List<phanquyen> thongtintk = BL.NhanVienBL.Loadthongtindangnhap_BL(stendangnhap, smk);
            string sMaNguoithu = thongtintk[0].MaNhanVien;
            bool bltrangthai = true;

            BL.HoaDonBL.Xacnhanthutien_BL(smahd, sMaNguoithu, bltrangthai);
        }

        //Ham load thong tin hoa don dang duoc chon
        void loadhoadondangchon()
        {
            string smabn = "";
            using (HMSHospitalEntities show = new HMSHospitalEntities())
            {
                smabn = (string)grdDShoadonchuathu.GetRowCellValue(grdDShoadonchuathu.FocusedRowHandle, grdDShoadonchuathu.Columns[0]);
                var chitiethd = (from cthd in show.HoaDons
                                 where cthd.MaBenhNhan == smabn && cthd.TrangThai == false
                                 select cthd).First<HoaDon>();
                txtMabn_XacNhanThuTien.Text = chitiethd.MaBenhNhan;
                txtTenbn_XacNhanThuTien.Text = chitiethd.BenhNhan.HoBenhNhan + " " + chitiethd.BenhNhan.TenBenhNhan;
                txtTuoi_XacNhanThuTien.Text = chitiethd.Tuoi.ToString();
                if (chitiethd.GioiTinh == true)
                    cmbGioiTinh_XacNhanThuTien.SelectedIndex = 0;//0 la nam
                else
                    cmbGioiTinh_XacNhanThuTien.SelectedIndex = 1;//1 la nu
                grcThongtinbienlai_XacNhanThuTien.DataSource = xacnhantt.LoaddsHoaDon_DA(smabn) ;

            }

        }

        int i = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            i++;
            if (i == 30)
            {
                Loaddshoadon();
                i = 0;
            }
        }

        private void txtTiennhan_XacNhanThuTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            int index = grddsHoaDon_XacNhanTT.FocusedRowHandle;
            grddsHoaDon_XacNhanTT.DeleteRow(index);
            int itong = 0;
            for (int i = 0; i < grddsHoaDon_XacNhanTT.RowCount; i++)
            {
               itong = itong + int.Parse(grddsHoaDon_XacNhanTT.GetRowCellValue(i, grddsHoaDon_XacNhanTT.Columns[2]).ToString());
            }
            txtTienphaitra_XacNhanThuTien.Text = String.Format("{0:0,0}", itong);

        }
    }
}
